package com.renwei.yunlong.activity.work;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 投诉反馈--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class ComplaintsFeedbackActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_user_name)
    TextView tvUserName;
    @Bind(R.id.rl_title)
    LinearLayout rlTitle;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    @Bind(R.id.tv_title_name)
    TextView tvTitleName;
    @Bind(R.id.iv_enter)
    ImageView ivEnter;
    private String complainId;
    private String classification;
    private String nowState;//状态
    private String newRequestId;//投诉生成工单ID
    private String employeeId = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_close);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        initData();
    }

    private void initData() {

        classification = getIntent().getStringExtra("classification");
        nowState = getIntent().getStringExtra("nowState");
        newRequestId = getIntent().getStringExtra("newRequestId");
        complainId = getIntent().getStringExtra("complainId");

        btnConfirm.setOnClickListener(this);
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etInputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int length = charSequence.length();
                tvInputNumber.setText(length + "");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        if ("反馈".equals(classification)) {
            simpleTileView.setTitle("反馈");
            tvTitleName.setText("投诉人");
            tvUserName.setText(getIntent().getStringExtra("sendUserName"));
            ivEnter.setVisibility(View.INVISIBLE);
            tvInputTitle.setText("反馈意见(必填)");
        } else if ("建议".equals(classification)) {
            simpleTileView.setTitle("建议");
            tvInputTitle.setText("建议原因(必填)");
            tvTitleName.setText("服务台");
            tvUserName.setHint("请选择");
            ivEnter.setVisibility(View.VISIBLE);
            rlTitle.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_confirm:
                String content = etInputContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    showCenterInfoMsg("请输入" + classification + "内容");
                    return;
                }
                if ("建议".equals(classification) && TextUtils.isEmpty(employeeId)) {
                    showCenterInfoMsg("请选择服务台");
                    return;
                }
                HttpPresenter hp = new HttpPresenter(this);
                HashMap<String, String> map = new HashMap<>();
                if ("反馈".equals(classification)) {
                    if ("1".equals(companyType)) {//业主
                        hp.setUrl(Api.$().OWNER_FANKUI_URL);
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {//服务商
                        hp.setUrl(Api.$().SERVICE_FANKUI_URL);
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                } else if ("建议".equals(classification)) {
                    if ("1".equals(companyType)) {//业主
                        hp.setUrl(Api.$().OWNER_FANKUI_URL);
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {//服务商
                        hp.setUrl(Api.$().$().SERVICE_FANKUI_URL);
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                    map.put("userids", employeeId); //
                }
                map.put("complainId", complainId);
                map.put("newRequestId", newRequestId);
                map.put("dealDetail", content);
                map.put("flag", nowState);
                hp.setshowDialogcontent("正在加载");
                hp.putParam("jsonBean", JsonMapUtil.mapToJson(map));
                hp.setCallBack(new HttpTaskListener() {
                    @Override
                    public void onSuccess(int requestId, String result) {
                        CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                        if (bean.getMessage().getCode() == 200) {
                            showCenterSuccessMsg("操作成功");
                            finish();
                            EventBus.getDefault().post(new ComplainPageRefreshEvent());
                        } else {
                            showCenterInfoMsg(bean.getMessage().getMessage());
                        }
                    }

                    @Override
                    public void onException(int requestId, String code) {

                    }
                });
                hp.post();
                break;
            case R.id.rl_title: // 选择处理人
                PersonSelectActivity.OpenActivity(mContext, PersonSelectActivity.FROM_COMPLAIN_DEAL, 111);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = null;
            if (data != null) {
                bundle = data.getExtras();
            }
            if (requestCode == 111) {
                tvUserName.setText(bundle.getString("name"));
                tvUserName.setTextColor(Color.parseColor("#333333"));
                employeeId = bundle.getString("employeeId");
            }
        }
    }
}
